//
//  main.m
//  05_homeWork
//
//  Created by 402030900@qq.com on 16/7/8.
//  Copyright © 2016年 402030900@qq.com. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Location.h"
int main(int argc, const char * argv[]) {

#if 0
// 一.
//    1.创建1个包含5个字符串元素的数组；
//    2.用for循环倒序输出所有元素；
//    3.用for...in完成所有字符串元素的拼接(生成一个新的字符串)，并输出拼接好的字符串。
    NSArray * OneArr = @[@"1",@"2",@"3",@"4",@"5"];
    NSMutableArray * reverseArr = [NSMutableArray array];
    for (NSInteger i = 4; i >= 0; i --) {
        [reverseArr addObject:OneArr[i]];
    }
    NSLog(@"倒序输出:%@",reverseArr);
    NSString * joinStr = [NSString string];
    for (NSString * temp in OneArr) {
       joinStr = [joinStr stringByAppendingString:temp];
    }
    NSLog(@"拼接好的字符串为:%@",joinStr);
#endif
#if 0
//    二、
//    
//    1.创建1个包含5个person对象的数组(姓名用英文)；
//    2.用for循环找到年龄最大的Person对象；
//    3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
    Person * person1 = [[Person alloc]initWithName:@"One" Age:55 Sex:@"M"];
    Person * person2 = [[Person alloc]initWithName:@"Two" Age:22 Sex:@"F"];
    Person * person3 = [[Person alloc]initWithName:@"Three" Age:33 Sex:@"M"];
    Person * person4 = [[Person alloc]initWithName:@"Four" Age:44 Sex:@"F"];
    Person * person5 = [[Person alloc]initWithName:@"Five" Age:11 Sex:@"M"];
    NSArray * personArr = @[person1,person2,person3,person4,person5];
    
    NSString * name = [NSString string];
    NSInteger age = 0;
    NSInteger i = 0;
   
    for (i = 0; i < personArr.count - 1; i++) {
        if ([personArr[i] age] >age) {
        age = [personArr[i] age];
        name = [personArr[i] name];
        }
    }
    NSLog(@"%@,%ld",name,age); // for循环找到年龄最大的Person对象；
    
    for (Person * person in personArr) {
        if (person.age > 44) {
           
        person.name =[person.name stringByAppendingString:@"-brother"];
        }
    }
     NSLog(@"%@",personArr);  // 大于某个年龄的Person对象的姓名后面拼接上字符串“-brother
#endif
#if 0
//    三、
//    * 1.使用第二题目中生成的数组；
//    * 2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
//    * 3.使用NSSortDescriptor，按姓名进行排序。
    
    NSSortDescriptor * sortNameDedcriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    NSArray * sortNameArr = [personArr sortedArrayUsingDescriptors:@[sortNameDedcriptor]];
    NSLog(@"NSSortDescriptor按姓名排序结果:%@",sortNameArr);   // 使用NSSortDescriptor，按姓名进行排序
    
     personArr = [personArr sortedArrayUsingSelector:@selector(compareNames:)];
    NSLog(@"sortUsingSelector按姓名排序结果:%@",personArr);// sortUsingSelector:按姓名进行排序
    personArr  = [personArr sortedArrayUsingSelector:@selector(compareSexs:)];
    NSLog(@"sortUsingSelector按性别排序结果:%@",personArr);// sortUsingSelector:按性别进行排序
    personArr  = [personArr sortedArrayUsingSelector:@selector(compareAges:)];
    NSLog(@"sortUsingSelector按年龄排序结果:%@",personArr);// // sortUsingSelector:按年龄进行排序
                         /****** age有问题 *********/
#endif
#if 0
//    四、
//    * 处理省市区文件中的area.txt
//    * 将省市区进行分割处理, 省里包含市, 市里包含区
//    1. 先用数组字典表示
//    2. 将数组字典的处理方式转换成数组模型(Class)


    NSString * resourceString = [NSString stringWithContentsOfFile:@"/Users/402030900qq.com/DLS160710/Objective-C/HomeWork/05_homeWork/05_homeWork/area.txt" encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"%@",resourceString);
    NSArray * resourceArray = [resourceString componentsSeparatedByString:@"\n"];
//    NSLog(@"%@",resourceArray);
    NSMutableArray * dataArr = [NSMutableArray array];
    for (NSString * temp in resourceArray) {
        if (![temp hasPrefix:@" "]) {
    NSMutableDictionary * proDic = [NSMutableDictionary dictionary];
          [proDic setObject:temp forKey:@"ProName"];
    NSMutableArray * cityArr = [NSMutableArray array];
          [proDic setObject: cityArr forKey:@"cityName"];
          [dataArr addObject:proDic];
        }else if ([temp hasPrefix:@"  "] && ![temp hasPrefix:@"    "]){
         
            NSMutableDictionary * proDic = [dataArr lastObject];
            NSMutableArray * cityArr = [proDic objectForKey:@"cityName"];
            [cityArr addObject:temp];
        }
        
        
    }
    NSLog(@"%@",dataArr);
#endif
//    五、拓展
//    * 上述作业做完, 处理Location1.txt
//    * 要求同第四题。自己寻找规律
#if 0
    NSString * path = @"/Users/402030900qq.com/DLS160710/Objective-C/HomeWork/05_homeWork/05_homeWork/Location1.txt";
    NSString * resource = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
   NSArray * resouceArr = [resource componentsSeparatedByString:@","];
   NSMutableArray * dataArr = [NSMutableArray array];
  
    for (NSString * number in resouceArr) {
        Location * location = [[Location alloc]init];
     NSArray * lineArr = [number componentsSeparatedByString:@"|"];
        location.name = [lineArr firstObject];
        location.number = [lineArr lastObject];
        [dataArr addObject:location];
    }
    NSMutableArray * localArr = [NSMutableArray array];
    for (Location * loc in dataArr) {
        if ([[loc.number substringFromIndex:loc.number.length - 4]isEqualToString:@"0000"] ) {
       NSMutableDictionary * proDic = [NSMutableDictionary dictionary];
        [proDic setObject:loc.name forKey:@"proName"];
        [proDic setObject:loc.number forKey:@"proNumber"];
        NSMutableArray * cityArr = [NSMutableArray array];
        [proDic setObject:cityArr forKey:@"cityName"];
        [localArr addObject:proDic];
        }else if (![[loc.number substringFromIndex:loc.number.length - 4]isEqualToString:@"0000"]&&[[loc.number substringFromIndex:loc.number.length - 3]isEqualToString:@"000"]){
            
            for (NSMutableDictionary * proDictionary in dataArr) {
                if ([proDictionary[@"proNumber"] isEqualToString:[loc.number substringToIndex:3]]) {
                    NSMutableArray * cityArr = [proDictionary objectForKey:@"cityName"];
                    NSMutableDictionary * cityDic = [NSMutableDictionary dictionary];
                    NSMutableArray * regionArr = [NSMutableArray array];
                    [cityDic setObject:loc.name forKey:@"cityName"];
                    [cityDic setObject:regionArr forKey:@"regionName" ];
                    [cityDic setObject:[loc.name substringToIndex:4] forKey:@"cityNumber"];
                    [cityArr addObject:cityDic];
                    
                }
                
                }
        }else{
            for (NSMutableDictionary * cityDictionary in dataArr) {
                NSMutableArray * cityArr = [cityDictionary objectForKey:@"cityName"];
                for (NSMutableDictionary * cityDic in cityArr) {
                    NSMutableArray * cityArr = [cityDictionary objectForKey:@"cityName"];
                    for (NSMutableDictionary * cityDic in cityArr) {
                        if ([cityDic[@"cityNumber"]isEqualToString:[loc.number substringToIndex:4]]) {
                            NSMutableArray * regionArr = [cityDic objectForKey:@"regionName"];
                            [regionArr addObject:loc.name];
                        }
                    }
                }
            }
        
        }
    }
    NSLog(@"%@",localArr);
#endif
    return 0;
}
